model_permute <- function(depvar, covars, data)
{
  combn_list <- list()
  for (i in seq_along(covars)) {
    combn_list[[i]] <- combn(covars, i, simplify = FALSE)
  }
  combn_list <- unlist(combn_list, recursive = FALSE)
  
  gen_formula <- function(covars, depvar) {
    form <- as.formula(paste0(depvar, " ~ ", paste0(covars, collapse = "+")))
    form
  }
  formulas_list <- lapply(combn_list, gen_formula, depvar = depvar)
  
  model_list <- lapply(formulas_list, lm, data = data)
  
  models <- lapply(model_list, broom::tidy)
  models_out <- lapply(models, function(x){dplyr::mutate(x, rhs = paste(x$term, collapse = " + "))})
  variables <- sapply(models, `[[`,1) %>% unlist()
  estimates <- sapply(models, `[[`,2) %>% unlist()
  std_error <- sapply(models, `[[`,3) %>% unlist()
  rhs <- sapply(models_out,`[[`, 6) %>% unlist()
  
  models_out <- tibble::tibble(variables, estimates, std_error, rhs)
}

#data from permute
data_from_permute <- function(measure, pos)
{
  cols_table <- c("var", "est_naive", "std_error_naive", "rhs",  "var2", "est_corr", "std_error_corr", "rhs2")
  permuted_df <- tibble::tibble(naive_list[[pos]], corrected_list[[pos]], .name_repair = ~cols_table) %>%
    dplyr::filter(var != "(Intercept)") %>%
    dplyr::select(-var2) %>%
    dplyr::mutate(numerator = (est_naive - est_corr),
                  denominator = sqrt((std_error_naive^2) + (std_error_corr^2)),
                  z = numerator/denominator,
                  index = measure)
  permuted_df
}

model_permute_pred <- function(formula_list, iteration, data) {
  model_list<- lapply(formula_list, lm, data = data)
  models <- lapply(model_list, broom::tidy, conf.int = T)
  models_out <- lapply(models, function(x){dplyr::mutate(x, rhs = paste(x$term, collapse = " + "))})
  variables <- lapply(models, `[[`,1) %>% unlist()
  estimates <- lapply(models, `[[`,2) %>% unlist()
  std_error <- lapply(models, `[[`,3) %>% unlist()
  p_value <- lapply(models, `[[`,5) %>% unlist()
  conf.low <- lapply(models, `[[`,6) %>% unlist()
  conf.high <- lapply(models, `[[`,7) %>% unlist()
  rhs <- lapply(models_out,`[[`, 8) %>% unlist()
  models_out <- tibble::tibble(variables, estimates, std_error, p_value, conf.low, conf.high, rhs) %>% dplyr::filter(stringr::str_detect(variables, ":")) %>%
    dplyr::mutate(dv = rep(dv_list, 6),
                  iv = stringr::str_extract(variables, stringr::regex("^(.+?):")),
                  iteration = iteration) %>% 
    dplyr::select(iteration, dv, iv, everything())
  
}

model_permute_lm <- function(formula_list, iteration, data) {
  model_list<- lapply(formula_list, lm, data = data)
  models <- lapply(model_list, broom::tidy, conf.int = T)
  models_out <- lapply(models, function(x){dplyr::mutate(x, rhs = paste(x$term, collapse = " + "))})
  variables <- lapply(models, `[[`,1) %>% unlist()
  estimates <- lapply(models, `[[`,2) %>% unlist()
  std_error <- lapply(models, `[[`,3) %>% unlist()
  p_value <- lapply(models, `[[`,5) %>% unlist()
  conf.low <- lapply(models, `[[`,6) %>% unlist()
  conf.high <- lapply(models, `[[`,7) %>% unlist()
  rhs <- lapply(models_out,`[[`, 8) %>% unlist()
  models_out <- tibble::tibble(variables, estimates, std_error, p_value, conf.low, conf.high, rhs) %>% dplyr::filter(stringr::str_detect(variables, ":")) %>%
    dplyr::mutate(dv = rep(dv_list, 6),
                  iv = stringr::str_extract(variables, stringr::regex("^(.+?):")),
                  iteration = iteration) %>% 
    dplyr::select(iteration, dv, iv, everything())
  
}